package com.yihubang.taobaokotlin.view

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import android.widget.RelativeLayout
import com.yihubang.taobaokotlin.R

/*
 * class title: 
 * Author by RenChunLin, Email 18957806320@163.com, Date on 2021/5/12.
 * PS: Not easy to write code, please indicate.
 */
class InputNumberView : RelativeLayout {

    private var max = 0
    private var min = 0
    private var step = 0
    private var disable = false
    private var btnBgRes = -1
    private var defaultValue = 0
    private var valueSize = 12f

    companion object {
        const val TAG = "CHEN"
    }

    var mCurrentNumber = 0
    lateinit var minusBtn: View
    lateinit var valueEdit: EditText
    lateinit var plusBtn: View

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        initView(context)
        //获取相关属性
        initAttrs(attrs)
        //设置事件
        setUpEvent()
    }

    private fun initAttrs(attrs: AttributeSet?) {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.InputNumberView)
        max = typedArray.getInt(R.styleable.InputNumberView_inputNumberMaxNumber, 0)
        min = typedArray.getInt(R.styleable.InputNumberView_inputNumberMinNumber, 0)
        step = typedArray.getInt(R.styleable.InputNumberView_inputNumberStep, 1)
        disable = typedArray.getBoolean(R.styleable.InputNumberView_inputNumberDisable, false)
        btnBgRes =
            typedArray.getResourceId(R.styleable.InputNumberView_inputNumberBtnBackground, -1)
        defaultValue = typedArray.getInt(R.styleable.InputNumberView_inputNumberDefaultValue, 0)
        valueSize =
            typedArray.getDimension(R.styleable.InputNumberView_inputNumberValueSize, 12f)
        typedArray.recycle()

        //初始化控件值
        this.mCurrentNumber = defaultValue
        updateText()
        minusBtn.isEnabled = !disable
        plusBtn.isEnabled = !disable
    }


    private fun initView(context: Context) {
        val view =
            LayoutInflater.from(context).inflate(R.layout.input_number_view, this, false)
        addView(view)

        minusBtn = view.findViewById(R.id.minus_btn)
        valueEdit = view.findViewById(R.id.value_edit)
        plusBtn = view.findViewById(R.id.plus_btn)
    }

    private fun setUpEvent() {
        minusBtn.setOnClickListener {
            mCurrentNumber -= step
            plusBtn.isEnabled=true
            if (mCurrentNumber <= min) {
                it.isEnabled=false
                mCurrentNumber = min
            }
            updateText()
        }

        plusBtn.setOnClickListener {
            mCurrentNumber += step
            minusBtn.isEnabled=true
            if (mCurrentNumber >= max) {
                it.isEnabled=false
                mCurrentNumber = max
            }
            updateText()
        }
    }

    fun getNumber() = mCurrentNumber

    fun setNumber(value: Int) {
        mCurrentNumber = value
        this.updateText()
    }

    fun getMax() = max

    fun setMax(value: Int) {

    }

    fun getMin() = min

    fun setMin(value: Int) {

    }

    fun getStep() = step

    fun setStep(value: Int) {

    }

    fun getDefaultValue() = defaultValue

    fun setDefaultValue(value: Int) {
        mCurrentNumber = value
        this.updateText()
    }

    /**
     * 更新数组
     */
    fun updateText() {
        if (::valueEdit.isInitialized) {
            valueEdit.setText(mCurrentNumber.toString())
            mOnNumberChangeListener?.onNumberChange(this.mCurrentNumber)
        }
    }

    var mOnNumberChangeListener: OnNumberChangeListener? = null

    fun setOnNumberChangeListener(listener: OnNumberChangeListener) {
        this.mOnNumberChangeListener = listener
    }

    interface OnNumberChangeListener {
        fun onNumberChange(value: Int)
    }
}